package com.example.principle.ocp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// 告警类
public class Alert {


   /* private AlertRule rule;
    private Notification notification;

    public Alert(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    public void adaptiveAlert(String api,long requstCount,long errorCount,long timeoutCount,long durationOfSeconds) {
        // tps
        long tps = requstCount / durationOfSeconds;
        if (tps > rule.getMatchedRule(api).getMaxTps()) {
            notification.notify(Notification.NotificationEmergencyLevel.URGENCY,".........");
        }
        // errorcout
        if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
            notification.notify(Notification.NotificationEmergencyLevel.URGENCY,"........");
        }

        // add timeoutCount logic
        // 1, add method parameters 2, add timeoutCount logic
        if (timeoutCount > rule.getMatchedRule(api).getMaxTimeoutCoun()) {
            //
        }


    }*/

    // manage api
    private List<AlertHandler> handlers = new ArrayList<>();

    public void addHandler(AlertHandler handler) {
        handlers.add(handler);
        Collections.sort(handlers);
    }
    // spi  spring

    // 顺序

    public void adaptiveAlert(ApiInfo apiInfo) {
        AlertContext context = new AlertContext();
        for (AlertHandler handler : handlers) {
            handler.adaptiveAlert(apiInfo,context);
        }
    }



    /*private List<AlertHandler> handlers = new ArrayList<>();

    public void addHandler(AlertHandler alertHandler) {
        handlers.add(alertHandler);
    }

    public void adaptiveAlert(ApiInfo apiInfo) {
        for (AlertHandler handler : handlers) {
            handler.adaptiveAlert(apiInfo);
        }
    }*/

}
